﻿using Newtonsoft.Json;
using Production.CommonLib.Constants;
using Production.CommonLib.Models;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;
using System.Net.Http.Json;

namespace Production.RazorLib.Services
{
    public class ProcessService : IProcessService
    {
        private readonly IHttpClientFactory _httpClientFactory;

        public ProcessService(IHttpClientFactory httpClientFactory)
        {
            _httpClientFactory = httpClientFactory;
        }

        public async Task<PaginatedResult<ProcessInfoResponseDto>?> QueryProcessesByPage(ProcessSearchRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("productionApi");
                var response = await httpClient.GetAsync(model.ToQueryString(ApiRoutes.Process.QueryProcesses));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<PaginatedResult<ProcessInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<ProcessInfoResponseDto>?> CreateProcess(ProcessEditRequestDto editRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("productionApi");
                var response = await httpClient.PostAsJsonAsync($"/{ApiRoutes.Process.CreateProcess}", editRequestDto);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<ProcessInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<ProcessInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<ProcessInfoResponseDto>?> GetProcess(int key)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("productionApi");
                var response = await httpClient.GetAsync($"/{ApiRoutes.Process.GetProcess.Replace("{id}", key.ToString())}");
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<ProcessInfoResponseDto>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<ProcessInfoResponseDto>?> UpdateProcess(int id, ProcessEditRequestDto editRequestDto)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("productionApi");
                var response = await httpClient.PutAsJsonAsync($"/{ApiRoutes.Process.UpdateProcess.Replace("{id}", id.ToString())}", editRequestDto);
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<ProcessInfoResponseDto>>(resultJson);
                    }
                }

                return new Result<ProcessInfoResponseDto>();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }

        public async Task<Result<IList<ProcessInfoResponseDto>>?> QueryProcesses(ProcessSearchRequestDto model)
        {
            try
            {
                var httpClient = _httpClientFactory.CreateClient("productionApi");
                var response = await httpClient.GetAsync(model.ToQueryString(ApiRoutes.Process.QueryProcesses));
                if (response.IsSuccessStatusCode)
                {
                    var resultJson = await response.Content.ReadAsStringAsync();

                    if (!string.IsNullOrEmpty(resultJson))
                    {
                        return JsonConvert.DeserializeObject<Result<IList<ProcessInfoResponseDto>>>(resultJson);
                    }
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}